package com.wenxixuanwu.fruitstore.service.impl;

import com.wenxixuanwu.fruitstore.entity.Cart;
import com.wenxixuanwu.fruitstore.entity.CartVO;
import com.wenxixuanwu.fruitstore.entity.Product;
import com.wenxixuanwu.fruitstore.entity.User;
import com.wenxixuanwu.fruitstore.mapper.BackProductMapper;
import com.wenxixuanwu.fruitstore.mapper.CartMapper;
import com.wenxixuanwu.fruitstore.mapper.ProductMapper;
import com.wenxixuanwu.fruitstore.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/5/7
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private BackProductMapper productMapper;
    @Override
    public List<CartVO> findVoByUid(Integer uid) {
        List<CartVO> result = cartMapper.findVoByUid(uid);
        List<CartVO> cartVOList = new ArrayList<>();
        if (result != null){
            for (CartVO carts : result) {
                CartVO cartVO = new CartVO();
                cartVO.setCid(carts.getCid());
                cartVO.setUid(carts.getUid());
                cartVO.setPid(carts.getPid());
                cartVO.setName(carts.getName());
                cartVO.setUrl(carts.getUrl());
                cartVO.setPrice(carts.getPrice());
                cartVO.setNum(carts.getNum());
                cartVO.setCreated(carts.getCreated());
                cartVO.setAllprice(carts.getPrice() * carts.getNum());
                cartVOList.add(cartVO);
            }
        }
        return cartVOList;
    }

    @Override
    public Integer addNum(Integer cid) {
        Cart result = cartMapper.findByCid(cid);
        Integer number = result.getNum() + 1;
        cartMapper.updateNumByCid(cid,number);
        return number;
    }

    @Override
    @Transactional
    public Integer addCart(Integer pid, HttpSession session) {
        Cart cart = new Cart();
        User user = (User) session.getAttribute("u");
        Integer uid = user.getId();
        Cart result = cartMapper.findByUidAndPid(uid,pid);
        if (result == null){
            cart.setUid(uid);
            Product product = productMapper.findProductById(pid);
            cart.setPid(pid);
            cart.setPrice(product.getOldPrice());
            cart.setNum(1);
            cart.setCreated(new Date());
            return cartMapper.insertCart(cart);
        }else{
            Integer cid = result.getCid();
            Integer num = result.getNum() + 1;
          return cartMapper.updateNumByCid(cid, num);
        }
    }

    @Override
    public Integer deleteCart(Integer cid) {
        return cartMapper.deleteCart(cid);
    }

    @Override
    public List<CartVO> getVOByCid(Integer[] cid, HttpSession session) {
        User user = (User) session.getAttribute("u");
        Integer userId = user.getId();
        List<CartVO> list = cartMapper.getCartVOByCid(cid);
        Iterator<CartVO> it = list.iterator();
        while (it.hasNext()){
            CartVO cart = it.next();
            if (!cart.getUid().equals(userId)){
                it.remove();
            }
        }
        //while语句等价于 list.removeIf(cart -> !cart.getUid().equals(userId));
        for (CartVO item : list){
            item.setAllprice(item.getPrice() * item.getNum());
        }
        return list;
    }
}
